import path from 'path';

import { expect } from '@jest/globals';
import fse from 'fs-extra';

import {runHvigorAsync} from '../../common/common';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
} from '../../plugins/project-builder/models/constants';
import { Project } from '../../plugins/project-builder/models/project-models';
import { ICaseRecord } from '../../plugins/report-builder/report-models';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOption, TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility,
  },
  modulesOption: {
    [ModuleTemplateType.StaticLibrary]: 1,
  },
};

/**
 *
 * @param {Project} project
 * @param {TestTaskOption} taskConfig
 * @param {ICaseRecord} record
 * @return res
 */
const testFun: TestCaseFun = async (project: Project, taskConfig: TestTaskOption, record: ICaseRecord) => {
  const projectRoot = project.projectRootPath();
  const moduleRootPath = project.getModule('staticLibrary').moduleRootPath();

  // localTest的产物
  const artifactRoot = path.resolve(moduleRootPath, '.test/default/cache/default/default@UnitTestArkTS/esmodule/debug/staticLibrary/Index.ts');

  // UnitTestBuild withCoverage 没有单独withCoverage的命令
  const commands1 = '--mode module -p module=staticLibrary@default -p unit.test.replace.page=../../../.test/testability/pages/Index -p product=default -p pageType=page -p unitTestMode=true -p ohos-test-coverage=true -p buildRoot=.test UnitTestBuild'.split(' ');
  const result1 = await runHvigorAsync(projectRoot, commands1);
  record.addStep('执行UnitTestBuild withCoverage', result1);
  expect(fse.existsSync(artifactRoot)).toEqual(true);
  console.log("11111111111111111111111");

  // UnitTestBuild isLocalTest=true 没有单独isLocalTest=true的命令
  const commands2 = '--mode module -p module=staticLibrary@default -p unit.test.replace.page=../../../.test/testability/pages/Index -p product=default -p pageType=page -p isLocalTest=true -p unitTestMode=true -p buildRoot=.test UnitTestBuild'.split(' ');
  const result2 = await runHvigorAsync(projectRoot, commands2);
  record.addStep('执行UnitTestBuild isLocalTest=true', result2);
  expect(fse.existsSync(artifactRoot)).toEqual(true);
  console.log("2222222222222222222222222");

  // UnitTestBuild
  const commands3 = '--mode module -p module=staticLibrary@default -p unit.test.replace.page=../../../.test/testability/pages/Index -p product=default -p pageType=page -p unitTestMode=true -p buildRoot=.test UnitTestBuild '.split(' ');
  const result3 = await runHvigorAsync(projectRoot, commands3);
  record.addStep('执行UnitTestBuild', result3);
  expect(fse.existsSync(artifactRoot)).toEqual(true);

  // 命令行Terminal指令以及ohos_test的当前e2e覆盖不到

};

TestCaseBuilder.builder()
  .caseName('DTS2024110704507')
  .description('配合测试框架检测打桩产物是否为.ts文件')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.DTS).build())
  .testFun(testFun)
  .load();
